package com.zjw.zy.utils

import android.graphics.drawable.Drawable
import com.zjw.zy.R
import com.zjw.zy.bean.DailyWeather
import com.zjw.zy.bean.HourlyWeather
import com.zjw.zy.constant.WeatherConstant
import kotlin.random.Random

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/28 17:03
 */
object WeatherUtils {

    private var mCurrentBackgroundId = -1;

    private val SUN_DRAWABLE_ARRAY: List<Int> = ArrayList<Int>().apply {
        add(R.drawable.main_weather_background_sun_1)
        add(R.drawable.main_weather_background_sun_2)
        add(R.drawable.main_weather_background_sun_3)
    }

    private val SUN_NIGHT_DRAWABLE_ARRAY: List<Int> = ArrayList<Int>().apply {
        add(R.drawable.main_weather_background_night_sun_1)
        add(R.drawable.main_weather_background_night_sun_2)
        add(R.drawable.main_weather_background_night_sun_3)
    }

    private val RAIN_DRAWABLE_ARRAY: List<Int> = ArrayList<Int>().apply {
        add(R.drawable.main_weather_background_rain_1)
        add(R.drawable.main_weather_background_rain_2)
        add(R.drawable.main_weather_background_rain_3)
        add(R.drawable.main_weather_background_rain_4)
    }

    private val OVERCAST_DRAWABLE_ARRAY: List<Int> = ArrayList<Int>().apply {
        add(R.drawable.main_weather_background_overcast_1)
        add(R.drawable.main_weather_background_overcast_2)
        //add(R.drawable.main_weather_background_overcast_3)
    }
    private val CLOUD_DRAWABLE_ARRAY: List<Int> = ArrayList<Int>().apply {
        add(R.drawable.main_weather_background_cloud_1)
    }

    private val RAIN_LIGHT_DRAWABLE_ARRAY: List<Int> = ArrayList<Int>().apply {
        add(R.drawable.main_weather_background_rain_light_1)
        add(R.drawable.main_weather_background_rain_light_2)
    }

    fun getWeatherBackground(weather: HourlyWeather): Drawable {
        var resId = -1

        //todo 待完善
        when (weather.text) {
            WeatherConstant.WEATHER_STATE_CLOUDY -> {
                resId = if (TimeUtils.isDay()) {
                    getRandomId(CLOUD_DRAWABLE_ARRAY)
                } else {
                    getRandomId(SUN_NIGHT_DRAWABLE_ARRAY)
                }
            }

            WeatherConstant.WEATHER_STATE_HEAVY_RAIN,
            WeatherConstant.WEATHER_STATE_LIGHT_RAIN,
            WeatherConstant.WEATHER_STATE_MODERATE_RAIN -> {
                resId = if (TimeUtils.isDay()) {
                    getRandomId(CLOUD_DRAWABLE_ARRAY)
                } else {
                    getRandomId(SUN_NIGHT_DRAWABLE_ARRAY)
                }
            }
            WeatherConstant.WEATHER_STATE_SUNNY -> {
                resId = if (TimeUtils.isDay()) {
                    getRandomId(SUN_DRAWABLE_ARRAY)
                } else {
                    getRandomId(SUN_NIGHT_DRAWABLE_ARRAY)
                }
            }

            WeatherConstant.WEATHER_STATE_OVERCAST -> {
                resId = if (TimeUtils.isDay()) {
                    getRandomId(OVERCAST_DRAWABLE_ARRAY)
                } else {
                    getRandomId(SUN_NIGHT_DRAWABLE_ARRAY)
                }
            }
            else -> {
                resId = if (TimeUtils.isDay()) {
                    getRandomId(SUN_DRAWABLE_ARRAY)
                } else {
                    getRandomId(SUN_NIGHT_DRAWABLE_ARRAY)
                }
            }
        }
        mCurrentBackgroundId = resId
        return ResourcesUtils.getDrawable(resId)
    }

    fun getCurrentWeatherBackGround(): Drawable? {
        if (mCurrentBackgroundId != -1) {
            return ResourcesUtils.getDrawable(mCurrentBackgroundId)
        }
        return null
    }

    private fun getRandomId(array: List<Int>): Int {
        val size = array.size
        val index = Random.nextInt(0, size)
        return array[index]
    }


    fun getWeatherIcon(weather: HourlyWeather): Drawable {
        return ResourcesUtils.getDrawable(getWeatherIconResId(weather.text, weather.isDay))
    }

    fun getWeatherIcon(weather: DailyWeather): Drawable {
        return ResourcesUtils.getDrawable(getWeatherIconResId(weather.textDay))
    }

    fun getWeatherIconResId(text: String?, isDay: Boolean = true): Int {
        var resId = -1

        //todo 待完善
        when (text) {
            WeatherConstant.WEATHER_STATE_CLOUDY -> {
                resId = R.drawable.weather_icon_cloud1
            }

            WeatherConstant.WEATHER_STATE_HEAVY_RAIN,
            WeatherConstant.WEATHER_STATE_LIGHT_RAIN,
            WeatherConstant.WEATHER_STATE_MODERATE_RAIN -> {
                resId = R.drawable.weather_icon_rain2
            }
            WeatherConstant.WEATHER_STATE_SUNNY -> {
                resId = R.drawable.weather_icon_sun1
            }

            WeatherConstant.WEATHER_STATE_OVERCAST -> {
                resId = R.drawable.weather_icon_y1
            }
            else -> {
                resId = R.drawable.weather_icon_sun1
            }
        }

        if (!isDay) {
            //夜色图标
            resId = R.drawable.weather_icon_night_sun_1
        }
        return resId
    }

}